home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Tool Chest / Testing & Debugging / Virtual User / Virtual User Current Release / Examples / Example Libraries / UtilityTasks.vulib < prev   
Encoding:
Text File  |  1998-06-04  |  20.4 KB  |  587 lines  |  [TEXT/MPS ]

  1. #
  2. #    File:        UtilityTasks.vulib
  3. #
  4. #    Contains:    contains utility tasks for any scripts. Use Mark menu to
  5. #                reach the desired task. The tasks are commented on what they do.
  6. #
  7. #    Written by:    P Nagarajan
  8. #
  9. #    Copyright:    © 1991-1996 by Apple Computer, Inc., all rights reserved.
  10. #
  11. #    Change History (most recent first):
  12. #
  13. #         <8>    2/28/96        CMW            Heavily modified FindFile to not use hardcoded ords,
  14. #                                        removed selection of "More Choices" button, and
  15. #                                        rearranged actions.
  16. #         <8>    960227        JCU            Modified FindFile to reflect ordinality change of popups.
  17. #                8/14/92        DGG            Modified CloseAllWindows and OpenOneWindow tasks.
  18. #         <7>     7/1/92        DGG            Included modified RestartTarget task and twitching
  19. #                                        tasks, closeAllWindows task and reformatted 
  20. #                                        existing tasks.
  21. #         <6>     8/26/91    Rick        Marked new tasks
  22. #         <5>     8/19/91    Rick        Fix LaunchAppInSystem7
  23. #         <4>     8/19/91    Rick        Fix timing in LaunchAppInSystem7() task
  24. #         <3>     8/19/91    Rick        Add FindWindowWithNoItems for 1.1 release
  25. #         <2>     8/13/91    Rick        fixed OpenOneWindow to account for 7.0 Desktop window
  26. #                   1/7/91    naga        creation 
  27. #
  28. #    To Do:
  29. #
  30.  
  31. (************************************************************************************
  32. * Task CheckForAlertWindow(overrideAlerts, messagePrint) 
  33. *    Handles alert dialogs with 'OK' and 'Cancel' buttons.
  34. *    if overrideAlerts is true then select 'OK' else 'Cancel' is selected
  35. ************************************************************************************)
  36. task CheckForAlertWindow(overrideAlerts := false, messagePrint := true) 
  37. begin
  38.     if (match [button w:[window ord:1 style:dialog] t:'OK']!) 
  39.     begin
  40.         textMessages := collect[staticText w:[window ord:1 style:dialog]]!;
  41.         if messagePrint
  42.         begin
  43.             println "### Dismissing dialog -- static text message follows:";
  44.             for each singleMessage in textMessages 
  45.                 println " ∂t",singleMessage.t;
  46.         end;
  47.         if overrideAlerts 
  48.             select [button t:'OK' w:[window ord:1 style:dialog]]!;
  49.         else 
  50.             select [button t:'Cancel' w:[window ord:1 style:dialog]]!;
  51.     end;#if dialog appeared
  52. end;#CheckForAlertWindow
  53.  
  54. (************************************************************************************
  55. * Task DismissConfirmDialog() 
  56. *    Handles alert dialogs with "No" buttons or any button beginning with "Don" 
  57. *    buttons.  Will simply click on these labeled buttons.
  58. ************************************************************************************)
  59. task DismissConfirmDialog()
  60. begin
  61.     if (match [window o:1 s:dialog])
  62.     begin
  63.         match [staticText t:?theText w:[window o:1]];
  64.         if ((theText ~= /Save≈/) or (theText ~= /Do you want to save≈/))
  65.             if (match [button t:"No"])
  66.                 select [button t:"No"];
  67.             else if (match [button t:/Don≈/])
  68.                 select [button t:/Don≈/];
  69.     end;
  70. end;
  71.  
  72. (************************************************************************************
  73. * Task CloseAllWindows() 
  74. *    Will attempt to close all current windows.  First it will look for dialogs or
  75. *    movable modals and try to close them, then it will close windows until there are
  76. *    no more windows to close.
  77. ************************************************************************************)
  78. task CloseAllWindows()
  79. begin
  80.     totalWindows := collect [window];
  81.     match [application t:?currentApp];
  82.     
  83.     for each singleWindow in totalWindows do
  84.     begin
  85.         if (singleWindow.s = dialog) or (singleWindow.s = movablemodal)
  86.         begin
  87.             if match [button t:'OK' w:1]!
  88.             begin
  89.                 select [button t:'OK' w:1]!;
  90.                 if not (currentApp = "Finder")
  91.                     DismissConfirmDialog();
  92.             end;
  93.             else if match [button t:'Cancel' w:1]!
  94.             begin
  95.                 select [button t:'Cancel' w:1]!;
  96.                 if not (currentApp = "Finder")
  97.                     DismissConfirmDialog();
  98.             end;
  99.         end;
  100.         else if not (                                      # skip 7.0 Finder desktop window
  101.                     match [system v:/7.≈/]!                # system 7    ?
  102.                     and currentApp = "Finder"    # finder ?
  103.                     and match [window t:"Desktop" o:1]!     # Desktop window ?
  104.                     )
  105.         begin
  106.             if (singleWindow.c)
  107.                 close [window o:1];
  108.             else if match [menuItem m:"File" t:/Close≈/ e:true ]
  109.                 select [menuItem m:"File" t:/Close≈/ e:true ];
  110.             else
  111.                 println "### Unable to close window: ", singleWindow.t;
  112.             if scriptError()
  113.                 println "### Unable to close window: ", singleWindow.t;
  114.             if not (currentApp = "Finder")
  115.                 DismissConfirmDialog();
  116.         end;
  117.     end; 
  118. end;
  119.  
  120. (************************************************************************************
  121. * Task FindFile(fileName)
  122. *    This task will find a file using the "Find…" menu item in the File menu, using
  123. *    name contains and on all disks at once.  It assumes that the Finder is currently
  124. *    active.
  125. ************************************************************************************)
  126. task FindFile(fileName := "", FindBy := "Name", MatchType := "Contains", Disk := "on all disks")
  127. begin
  128.  
  129.     foundFile := true;
  130.  
  131.     select [menuItem t:"Find…" m:[menu o:2]];
  132.     Wait(2);
  133.     
  134.     while (match [window t:/Find≈/]!).o <> 1
  135.     begin
  136.         if (match [window o:1]!).c = true
  137.             close [window o:1]!;
  138.         else
  139.             select [button t:"Ok" w:[window o:1]];
  140.     end;
  141.     
  142.     while match [button t:"Fewer Choices" w:[window t:/Find≈/ o:1]]!
  143.     begin
  144.         select [button t:"Fewer Choices" w:[window t:/Find≈/ o:1]]!;
  145.         Wait(1);
  146.     end;
  147.     
  148.     # There is a problem in the Find dialog where the "Find By" popup has all
  149.     #    of its menuitems removed if a "Fewer Choices" button is selected.
  150.     # The menuitems are not recomputed until another popup is selected.
  151.     # So we select the location popup and THEN the Find By.
  152.     
  153.     select [menuItem t:Disk m:[popup w:1]];            #select [menuItem t:Disk m:[popup o:18]];        # On all disks
  154.     select [menuItem t:FindBy m:[popup w:1]];                                                        # Name
  155.     type k:{ fileName };    
  156.     select [menuItem t:MatchType m:[popup w:1]];    #select [menuItem t:MatchType m:[popup o:6]];    # Is
  157.     
  158.     select [button t:"Find" w:[window t:/Find≈/ o:1]];    # Click Find
  159.  
  160.     stillSearching := true;
  161.     # A search may not find anything or it may take a long time.  This is one way 
  162.     # of dealing with the different outcomes.
  163.     while stillSearching
  164.     begin
  165.         Wait(2);
  166.         thisWindow := match [window o:1];
  167.         if (thisWindow.s = dialog)
  168.             stillSearching := false;
  169.         else if (thisWindow.t ~= /Find≈/)
  170.             stillSearching := true;
  171.         else
  172.             stillSearching := false;
  173.     end;
  174.  
  175.     if (thisWindow.s = dialog)
  176.     begin
  177.         if not (match [staticText t:/“{fileName}” was found on the≈/ w:[window s:dialog o:1]])        
  178.             foundFile := false;
  179.         select [button t:"Ok" w:[window o:1]];
  180.     end;
  181.     
  182.     return foundFile;
  183. end;
  184.  
  185. (************************************************************************************
  186. * Task FindObjectInWindow( atX, atY )
  187. *    this task moves the mouse on to a particular object of interest. Very useful 
  188. *    to select files in "view by name" mode in Finder.
  189. *    this assumes that the file/icon/object you want to select is on the frontmost 
  190. *    window with coordinates (atX, atY) relative to the window's origin
  191. ************************************************************************************)
  192. task FindObjectInWindow(atX, atY)
  193. begin
  194.     match [window o:1 r:?rect]!;
  195.     iconX := rect[1] + atX;
  196.     iconY := rect[2] + atY;
  197.     move a:{iconX, iconY};
  198. end; #FindObjectInWindow
  199.  
  200. (************************************************************************************
  201. * Task FindWindowWithNoItems()
  202. *    Returns a full descriptor of a window with an empty content list
  203. *    If no such window is found, the empty descriptor is returned
  204. ************************************************************************************)
  205. task FindWindowWithNoItems()
  206. begin
  207.     allWindows := collect[window];                # Gat a list of all windows
  208.     for each W in all_Window do                    # Search for an empty window
  209.         if Card W.k = 0                            # Is this one empty?
  210.             return W;                            # If so, return its descriptor
  211.  
  212.     #    If none found, return empty descriptor
  213.     return [];
  214. end; #FindWindowWithNoItems
  215.  
  216. (************************************************************************************
  217. * Task GracefulExit( msg )
  218. *    Exits VU after printing the supplied 'msg' along with script execution time
  219. ************************************************************************************)
  220. task GracefulExit(msg := '')
  221. begin
  222.     println msg;
  223.     if (TypeOf( global startTime ) <> 'undefined')
  224.         println "## started at  : ", global startTime;
  225.     println "## finished at : ", match [time];
  226.     exit;
  227. end; #GracefulExit
  228.  
  229. (************************************************************************************
  230. * Task NumberOfWindows()
  231. *    Returns the total number of open windows
  232. *    In system 7.0's Finder the Desktop is considered a window. This task
  233. *    ignores that window and returns the number of regular open windows.
  234. ************************************************************************************)
  235. task NumberOfWindows()
  236. begin
  237.     allWindows := collect [window];
  238.     match [application t:?appName]!;
  239.     if not (global System7) 
  240.          return card allWindows;
  241.     else if global System7 and (appName ~= /≈Finder≈/ ) 
  242.         return card allWindows - 1;
  243. end; #NumberOfWindows
  244.  
  245. (************************************************************************************
  246. * Task OpenOneWindow()
  247. *    This task leaves one window open in the Finder.  It performs in a round about
  248. *    way, first closing all windows and then trying to open one.  This task uses 
  249. *    the CloseAllWindows task.
  250. ************************************************************************************)
  251. task OpenOneWindow()
  252. begin
  253.     success := true;
  254.  
  255.     numWindows := NumberOfWindows();
  256.     if ( numWindows = 1 )
  257.         return success;
  258.     else if ( numWindows > 1)
  259.         CloseAllWindows();
  260.  
  261.     numWindows := NumberOfWindows();
  262.     if ( numWindows = 0 ) 
  263.     begin
  264.         if (match [menuItem t:'Open' m:'File' e:true]!)
  265.         begin
  266.             select [menuItem t:'Open' m:'File']!;
  267.             success := NumberOfWindows();
  268.         end; #if a window can be opened thru' the menu
  269.         else do
  270.             success := false;
  271.     end; #no windows currently
  272.     else do #could not close all windows
  273.     begin
  274.         match [window t:?topWindowTitle o:1];
  275.         if (topWindowTitle <> "Desktop")
  276.             success := false;
  277.     end;
  278.     return success;
  279. end; #OpenOneWindow
  280.  
  281. (************************************************************************************
  282. * Task RunningSystemSeven()
  283. *    Is the target running System 7.0.x?
  284. ************************************************************************************)
  285. task RunningSystemSeven()
  286. begin
  287.     match [system v:?sysVersion];
  288.     return ((sysVersion ~= /7≈/) or (sysVersion ~= /≈-7≈/));     # 7.x system software 
  289.                                                                 # on the target
  290. end; #RunningSystemSeven
  291.  
  292. (************************************************************************************
  293. * Task TwitchAppInSystem6( appName, twitchWait )
  294. *    This will select a currently running application in System 6 by "twitching" the
  295. *    application's name in the Apple menu. The application needs to be currently 
  296. *    running!  Also, it is assumed that MultiFinder is running on the target!
  297. *************************************************************************************)
  298. task TwitchAppInSystem6(appName := "Finder", twitchWait := 1)
  299. begin
  300.     match [application t:?frontApp]!;
  301.     if not (frontApp ~= /≈{appName}≈/)
  302.     begin
  303.         if match [menuItem t:/≈{appName}≈/ m:[menu o:1]]
  304.             select [menuItem t:/≈{appName}≈/ m:[menu o:1]];
  305.         Wait(twitchWait);
  306.         match [application t:?frontApp]!;
  307.         if not (frontApp ~= /≈{appName}≈/)
  308.         begin
  309.             println "### Failed to twitch to {appName}.";
  310.             return false;
  311.         end;
  312.     end;
  313.     return true;
  314. end;
  315.  
  316. (************************************************************************************
  317. * Task TwitchAppInSystem7( appName, twitchWait )
  318. *    This will select a currently running application in System 7 by "twitching" the
  319. *    menu item in the right most menu.  The application needs to be currently running!
  320. *************************************************************************************)
  321. task TwitchAppInSystem7(appName := "Finder", twitchWait := 1)
  322. begin
  323.     match [application t:?frontApp]!;
  324.     if not (frontApp ~= /≈{appName}≈/)
  325.     begin
  326.         numberOfMenus := card collect [menu];
  327.         if match [menuItem t:/≈{appName}≈/ m:[menu o:numberOfMenus]]
  328.             select [menuItem t:/≈{appName}≈/ m:[menu o:numberOfMenus]];
  329.         Wait(twitchWait);
  330.         match [application t:?frontApp]!;
  331.         if not (frontApp ~= /≈{appName}≈/)
  332.         begin
  333.             println "### Failed to twitch to {appName}.";
  334.             return false;
  335.         end;
  336.     end;
  337.     println "# Twitched to {appName}.";
  338.     return true;
  339. end;
  340.  
  341. (************************************************************************************
  342. * Task TwitchApp( appName, twitchWait )
  343. *    This will select a currently running application in either System 6 or 7 by 
  344. *    "twitching" app icon or appropriate the menu item in the right most menu.  The 
  345. *    application needs to be currently running!
  346. *************************************************************************************)
  347. task TwitchApp(appName := "Finder", twitchWait := 1)
  348. begin
  349.     if (RunningSystemSeven())
  350.         return TwitchAppInSystem7(appName, twitchWait);
  351.     else
  352.         return TwitchAppInSystem6(appName, twitchWait);
  353. end;
  354.  
  355. (************************************************************************************
  356. * Task LaunchAppInSystem6( appName, launchWait )
  357. *    Check for app running and/or attempt to get there by:
  358. *    double-clicking the topmost file (in View by Name) in the frontmost window 
  359. *    in Finder. ( Intended for use under System 6.0.x ) 
  360. *    launchWait is the number of seconds to wait before checking for successful
  361. *    launch of the application.
  362. ************************************************************************************)
  363. task LaunchAppInSystem6( appName := "", launchWait := 4 )
  364. begin
  365.     match [application t:?frontApp]!;
  366.     if (not (frontApp ~= /≈{appName}≈/))
  367.     begin
  368.         if not (TwitchAppInSystem6(appName))
  369.         begin
  370.             if (not OpenOneWindow()) 
  371.             begin
  372.                 println "### Could not launch {appName}";
  373.                 return false;
  374.             end;
  375.             select [menuItem t:'by Name' m:'View'];
  376.             All_Scrollbars := collect [ scrollbar w:1 ];
  377.             for each sb in All_Scrollbars
  378.                 if( sb.r[2] < 32 )
  379.                     Scroll sb a:{ 0, 1 };
  380.             FindObjectInWindow( 9, 46);#first file
  381.             DoubleClick;
  382.             Wait(launchWait); 
  383.             match [application t:?frontApp]!;
  384.             if not (frontApp ~= /≈{appName}≈/) 
  385.             begin
  386.                 println "### Failed to launch {appName}";
  387.                 return false;
  388.             end;
  389.         end;
  390.     end; 
  391.     println "### Launched {appName}";
  392.     return true;
  393. end; #LaunchAppInSystem6
  394.  
  395. (************************************************************************************
  396. * Task LaunchAppInSystem7( appName, launchWait, closeWindow, CDEV )
  397. *    Check for app running and/or attempt to get there by:
  398. *    Using Finder's Find command to select the App, then using the open command.
  399. *    ( This works only under System 7.0 )
  400. *    launchWait is the number of seconds to wait before checking for successful
  401. *    launch of the application.
  402. ************************************************************************************)
  403. task LaunchAppInSystem7( appName := "", launchWait := 6, closeWindow := true, 
  404.                         CDEV := false )
  405. begin
  406.     if (not CDEV)
  407.         match [application t:?frontApp]!;
  408.     else
  409.         match [window t:?frontApp o:1]!;
  410.         
  411.     if (not (frontApp = appName)) 
  412.     begin
  413.         if (not TwitchAppInSystem7(appName))
  414.         begin
  415.             TwitchAppInSystem7();                # Get to Finder!
  416.             if (not OpenOneWindow()) 
  417.             begin
  418.                 println "### Could not launch {appName}";
  419.                 return false;
  420.             end;    
  421.                 
  422.             if (FindFile( appName, "Name", "Contains" ))
  423.             begin
  424.                 if closeWindow
  425.                     pressKey k:{optionKey};
  426.                 select [menuItem t:"Open" m:[menu o:2]];
  427.                 if closeWindow
  428.                     releaseKey k:{optionKey};
  429.                 Wait(launchWait);
  430.  
  431.                 if (not CDEV)
  432.                 begin
  433.                     match[application t:?frontApp]!;
  434.                     if not (frontApp ~= /≈{appName}≈/) 
  435.                     begin
  436.                         println "### Failed to launch {appName}";
  437.                         return false;
  438.                     end;
  439.                 end;
  440.                 else 
  441.                 begin
  442.                     match [window t:?frontCDEV o:1];
  443.                     if not (frontCDEV ~= /≈{appName}≈/)
  444.                     begin
  445.                         println "### Failed to launch {appName}";
  446.                         return false;
  447.                     end;
  448.                 end;
  449.             end;
  450.             else
  451.             begin
  452.                 println "### Failed to find {appName}";
  453.                 println "### Failed to launch {appName}";
  454.                 return false;
  455.             end;
  456.         end;        
  457.     end;
  458.     println "### Launched {appName}";
  459.     return true;
  460. end; #LaunchAppInSystem7
  461.  
  462. (************************************************************************************
  463. * Task LaunchApp( appName, launchWait, closeWindow, CDEV)
  464. *    This will launch the specified application after determining whether System 6 or
  465. *    System 7 is being used on the target.
  466. *************************************************************************************)
  467. task LaunchApp(appName := "", launchWait := 6, closeWindow := true, CDEV := false )
  468. begin
  469.     if (RunningSystemSeven())
  470.         return LaunchAppInSystem7(appName, launchWait, closeWindow, CDEV);
  471.     else
  472.         return LaunchAppInSystem6(appName, launchWait);
  473. end;
  474.  
  475. (************************************************************************************
  476. * Task RestartTarget(timeToRestart := 15)
  477. *    This task will restart the current target machine.  It assumes that Finder is 
  478. *    the current application.  The timeToRestart parameter specifies the amount of 
  479. *    time to wait until the Virtual User tries to reacquire the target.
  480. ************************************************************************************)
  481. task RestartTarget(timeToRestart := 15)
  482. begin
  483.     # The following are error codes that are returned by acquireTarget and releaseTarget
  484.     noError := 0;
  485.     targetNotFound := 3; #error codes returned by acquireTarget/releaseTarget
  486.     targetFailure := 1;   #error codes returned by acquireTarget/releaseTarget
  487.     
  488.     # Get the target name and zone through unification.
  489.     match [target t:?targetName z:?targetZone];
  490.     
  491.     # Do restart via menu selection.
  492.     select [menuItem t:'Restart' m:'Special']!;
  493.     
  494.     # Do a release target _before_ the target restarts; this is essential for VU to 
  495.     #    realize that the the target under control has been lost. This then allows us
  496.     #    to do a fresh acquireTarget.
  497.     result := ReleaseTarget();
  498.  
  499.      if (result <> targetFailure)    # Released okay!
  500.     begin
  501.         (*
  502.          * Do a small wait to allow machine to boot.
  503.          * This step is not really required since we are going to keep trying to
  504.          * acquireTarget until it's found.  This step allows us to lessen network
  505.          * traffic.
  506.          *)
  507.         Wait(timeToRestart);
  508.         
  509.         # Do an acquire with the same old target name and zone address.
  510.         result := AcquireTarget(targetName, targetZone);
  511.  
  512.         while (result = targetNotFound)    # Keep trying until target is found or 
  513.         begin                                #    some other error is reported.
  514.             result := AcquireTarget(targetName, targetZone);
  515.         end;
  516.         if (result = noError)
  517.         begin
  518.             (*
  519.              * Need to wait until all inits get loaded.
  520.              * The following wait via menu match is not enough since the menu
  521.              * match succeeds before the finder really comes up visually.
  522.              * the caller of this restart task may have to do additional wait
  523.              * after this task call using a better heuristic based on the
  524.              * context(may be window match?)
  525.              *)
  526.             while not match[menu];
  527.             
  528.             return noError;
  529.         end;    # if (result = noError)
  530.         else 
  531.             return result;
  532.     end;    # if (result <> targetFailure)
  533.     return result;    # Error!  Couldn't release target!
  534. end;    #RestartTarget
  535.  
  536. (************************************************************************************
  537. * Task ThrowItemToTrash(printAnyError := true, overrideAlerts := false)
  538. *    This task trashes a specified object in Finder.
  539. *     Assumes that the mouse is already positioned on the item to be trashed
  540. *     and that the trash can is positioned on the main screen at (-40,-40) relative to
  541. *     the lower right corner.
  542. *     Arguements: 'printAnyError' can be used to suppress error messages and
  543. *                'overrideAlerts' can be used to trash despite alerts.
  544. ************************************************************************************)
  545. task ThrowItemToTrash(printAnyError := true, overrideAlerts := false)
  546. begin
  547.     PressMouse;
  548.     match [screen r:?rect m:true]!;
  549.     if rect 
  550.     begin
  551.         trash_x := rect[3] - 40; #assumes trash can is at its usual location
  552.         trash_y := rect[4] - 40; #ie., (40,40) off form the right bottom corner of main screen
  553.         old_mouse_speed := mouseSpeed(5); #slow the mouse down
  554.         move a:{trash_x, trash_y};
  555.         #move a:{472, 301};#trash can on a SE
  556.         mouseSpeed(old_mouse_speed); #reset the mouse speed to what it was before
  557.         ReleaseMouse;
  558.         CheckForAlertWindow(overrideAlerts);
  559.         if match [menuItem t:'Empty Trash' m:'Special' e:true]!
  560.             select [menuItem t:'Empty Trash' m:'Special']!;
  561.         else 
  562.         begin
  563.             if printAnyError
  564.                 println "### Empty Trash menu item not enabled, could not trash…";
  565.             return false;
  566.         end;
  567.     end;#if unified correctly
  568.     else 
  569.     begin
  570.         if printAnyError
  571.             println "### Unable to find trash can, could not trash…";
  572.         return false;
  573.     end;
  574.     return true;
  575. end; #ThrowItemToTrash
  576.  
  577. (************************************************************************************
  578. * Task UseKeyboardEquivalent( alias )
  579. *    Do Menu command using key equivalent
  580. ************************************************************************************)
  581. task UseKeyboardEquivalent(alias) 
  582. begin
  583.     pressKey k:{ commandKey };
  584.     type k:{ alias };
  585.     releaseKey k:{ commandKey };
  586. end; #UseKeyboardEquivalent
  587.